<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------


namespace app\user\admin;

use app\admin\admin\Base;
use app\user\model\UnsetType as UnsetTypeModel;
use service\Format;
/**
 * 注销类型控制器
 * @package app\UnsetType\admin
 */
class UnsetType extends Base
{
    /**
     * 注销类型列表
     * @author 似水星辰 [2630481389@qq.com]
     * @return mixed
     */
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = UnsetTypeModel::where($map)->order($order)->paginate();
        $fields =[
		    ['aid', 'ID', '', '', '', ],
            ['name', '类型名称', '', '', '', ],
            ['status', '状态', 'status'],
		    ['right_button', '操作', 'btn','','','text-center']
        ];
        return Format::ins() //实例化
            ->setPrimaryKey('aid')
            ->hideCheckbox()
            ->addColumns($fields)//设置字段
            ->setTopButtons($this->top_button)
            ->setRightButtons($this->right_button)
            ->setData($data_list)//设置数据
            ->fetch();//显示
    }

    /**
     * 新增
     * @author 似水星辰 [2630481389@qq.com]
     * @return mixed
     */
    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'UnsetType');
            if(true !== $result) $this->error($result);

            if ($page = UnsetTypeModel::create($data)) {
                $this->success('新增成功', cookie('__forward__'));
            } else {
                $this->error('新增失败');
            }
        }

        $fields =[
            ['type'=>'text', 'name'=>'name', 'title'=>'类型名称', ],

        ];
        $this->assign('page_title','新增注销类型');
        $this->assign('form_items',$fields);
        return $this->fetch('admin@public/add');
    }

    /**
     * 编辑
     * @param null $id 注销类型id
     * @author 似水星辰 [2630481389@qq.com]
     * @return mixed
     */
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
	        $data['update_time'] = time();

            // 验证
            $result = $this->validate($data, 'UnsetType');
            if(true !== $result) $this->error($result);

            if (UnsetTypeModel::update($data)) {
                $this->success('编辑成功', cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        }
	
        $info = UnsetTypeModel::get(['aid'=>$id]);
        $fields =[
            ['type' => 'hidden', 'name' => 'aid'],
            ['type'=>'text', 'name'=>'name', 'title'=>'类型名称', ],

        ];
        $this->assign('page_title','编辑注销类型');
        $this->assign('form_items', $this->setData($fields, $info));
        return $this->fetch('admin@public/edit');
    }
}